In this reading, you'll learn how to access and implement MySQL’s built-in functions using Python.

Accessing functions

Database engineers and developers need to perform several common tasks on data such as computing averages, sum, max, min, combining stings, counting records, handling date and time and many other similar operations. Writing code repetitively for such common tasks is not efficient and results in unnecessary repeat work.

A function is a piece of code that is written to return results after performing a specific task. MySQL comes with a range of built-in functions that are useful for database engineers and developers working with data-centric applications. These built-in functions can easily be embedded in your SQL query.

MySQL’s built-in functions include functions for working with strings. You can use these functions to concatenate strings, convert them into upper or lower case, find the lengths of strings, locate sub-strings, reverse strings and perform many more common operations on strings.

In the numerical functions category, you have functions to compute averages, sums, max, min, logs, and much more. There are also functions that are used to perform a range of trigonometric operations on the data.

MySQL also provides a set of functions that work with DateTime type of data. You can add a date and time or extract the day, year, month, hour, minutes and seconds using MySQL built-in functions from the DateTime object.

The advanced function in MySQL helps engineers to implement conditional statements using CASE. Using the NULLIF function, you can compare the two expressions and return NULL if they are equal. Another function CAST converts a value of any type into a specified datatype.

MySQL functions in a Python-based application

Next, let’s look at how to use MySQL functions in your Python-based application.

To access the MySQL functions, follow the SQL syntax and embed the function in your SQL query. For example, if you want to know the total sale in dollars for the Little Lemon restaurant, you can write the following query containing the function SUM :

sql_query = """SELECT SUM(BillAmont) AS Total_Sale FROM Orders;"""

The above query returns the total sale in dollars by adding up all the bill amounts in the BillAmount column from the Orders table.

You need to use the execute module from the cursor object and pass the SQL query as a Python string to run the above query in your Python-based application. You can use the following code to run the query:

cursor.execute(sql_query) 

Once the query is executed, you can fetch the results by calling a fetch module on the cursor :

results = cursor.fetchall()

The results contains the total sale in dollars. Print the results using a for loop and print statement from Python:

for result in results:
print(result)

Let’s consider another example of accessing the DateTime related function from MySQL. Little Lemon wants to know the peak hours of the restaurant so that they can plan their staff roster accordingly. To extract the hour data from the BookingSlot column of the Bookings table, your SQL query must include MySQL’s built-in function HOUR.

sql_query = """SELECT HOUR(BookingSlot) FROM Bookings;"""
cursor.execute(sql_query)
results = cursor.fetchall()

The results contain the hour data of the DateTime column BookingSlot from the Bookings table.

Conclusion

In this reading, you learned about embedding the MySQL built-in functions [SUM and HOUR] in your SQL query. You also learned how to execute that query to retrieve the required data from the MySQL database using Python.

You can now practice and use other MySQL built-in functions in your SQL query and use Python to extract the results.